
import cv2 as cv
from pyzbar import pyzbar as pyzbar
import sys

def detect(img):
    barcodes = pyzbar.decode(img)
    for barcode in barcodes:
        # 提取二维码的边界框的位置
        # 画出图像中条形码的边界框
        (x, y, w, h) = barcode.rect
        # 目标区域y1:y2,x1:x2
        img_dst = img[y - 5:y + h + 5, x - 5:x + w + 5]
    return img_dst, x, y, w, h


def process():
    img = cv.imread("G:\\python\\workspace\\venv\\images\\22.jpg")
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    img_dst, x, y, w, h = detect(gray)
    print('x=%d y=%d w=%d h=%d' % (x, y, w, h))
    # 将二维码区域保存
    cv.imwrite("G:\\python\\workspace\\venv\\images\\22-result.jpg", img_dst)
    # 用红色矩形框标记处二维码位置
    cv.rectangle(img, (x - 25, y - 25), (x + w + 25, y + h + 25), (0, 0, 255), 8)
    # 窗口自适应显示
    cv.namedWindow('result', cv.WINDOW_NORMAL)
    cv.imshow('result', img_dst)
    cv.waitKey(0)


if __name__ == '__main__':
    print(sys.argv[1], sys.argv[2], "xx")
